<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="id" />
      </el-form-item>

      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="昵称" />
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input v-model="queryParams.amount" placeholder="金额" />
      </el-form-item>
      <el-form-item label="开始日期" prop="startTime">
        <el-date-picker v-model="queryParams.startTime" type="datetime" placeholder="选择开始日期"
          value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>
      <el-form-item label="结束日期" prop="endTime">
        <el-date-picker v-model="queryParams.endTime" type="datetime" placeholder="选择结束日期"
          value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>
      <el-form-item label="订单编号">
        <el-input v-model="queryParams.orderNo" placeholder="订单编号" />
      </el-form-item>

      <el-form-item label="状态">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="提现方式">
        <el-select v-model="queryParams.type" placeholder="请选择">
          <el-option label="usdt" :value="0" />
          <el-option label="支付宝" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
        <el-button type="primary" @click="onExport">导出</el-button>

      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="序号" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>

      <el-table-column label="用户id" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注	" align="center" prop="remarks" />

      <el-table-column label="订单编号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.orderNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提现金额" align="center">
        <template slot-scope="scope">
          {{ scope.row.amount }}
        </template>
      </el-table-column>
      <el-table-column label="提现方式" align="center">
        <template slot-scope="scope">
          {{ scope.row.type == 0 ? 'usdt' : scope.row.type == 1 ? '支付宝' : '' }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>

      <el-table-column label="审核状态" align="center">
        <template slot-scope="scope">
          {{ setStateTxt(scope.row.status) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">审核</el-button>

        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination :current-page.sync="queryParams.pageNum" :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="fetchData" @current-change="fetchData" />
    </div>
    <showInfo ref="showInfo" />
  </div>
</template>

<script>
import { withdrawalList, exportExcel } from '@/api/table'
import showInfo from './showInfo.vue'
import axios from 'axios'
import { getToken } from '@/utils/auth'

export default {
  components: {
    showInfo
  },
  data() {
    return {
      list: null,
      listLoading: true,
      timer: null,
      total: 0,
      typeOptions: [
        { label: '虚拟货币', value: 'usdt' },
        { label: '人民币', value: 'rmb' }
      ],
      statusOptions: [
        { label: '申请中', value: 'applying' },
        { label: '审核通过', value: 'approved' },
        { label: '审核驳回', value: 'reject' }

      ],
      queryParams: {
        'hash': '',
        'orderNo': '',
        'pageNum': 1,
        'pageSize': 10,
        'status': '',
        'type': '',
        'userId': '',
        startTime: "",
        endTime: ""

      }
    }
  },
  created() {
    this.fetchData()
    this.queryParams.startTime = new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + ' ' + '00:00:00'
    this.queryParams.endTime = new Date().getFullYear() + '-' + (new Date().getMonth() + 1) + '-' + new Date().getDate() + ' ' + '23:59:59'
    // this.timer = setInterval(() => {
    //   this.fetchData()
    // }, 10000)
  },
  destroyed() {
    console.log('销毁')
    clearInterval(this.timer)
  },
  methods: {

    setStateTxt(index) {
      console.log(index)
      let str = ''
      switch (index) {
        case 'applying':
          str = '申请中'
          break
        case 'approved':
          str = '审核通过'
          break
        case 'reject':
          str = '审核驳回'
          break
      }
      return str
    },
    onExport() {
      if (!this.queryParams.startTime || !this.queryParams.endTime) {
        this.$confirm('请选择开始时间和结束时间?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        return false
      }
      axios({
        url: exportExcel(),
        params: { startTime: this.queryParams.startTime, endTime: this.queryParams.endTime },
        methods: 'get',
        headers: {
          Authorization: 'Bearer ' + getToken()
        },
        responseType: 'arraybuffer',

      }).then(res => {
        // 数据量比较大整个加载动画
        // 生成下载URL
        const url = window.URL.createObjectURL(new Blob([res.data]), {
          type: 'application/vnd.ms-excel',
          'charset': 'utf-8',
          'content-type': 'application/octet-stream'
        })
        // 创建隐藏的a标签用于自动下载导出文件
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', '提现' + '.xlsx')
        document.body.appendChild(link)
        link.click()


      })


    },
    fetchData() {
      this.listLoading = true
      withdrawalList(this.queryParams).then(response => {
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    handleShowInfo(e) {
      this.$refs.showInfo.show(e)
    },

    onSubmit() {
      this.queryParams.page = 1
      this.fetchData()
    },
    reset() {
      this.queryParams = {
        'hash': '',
        'orderNo': '',
        'pageNum': 1,
        'pageSize': 10,
        'status': '',
        'type': ''
      }
      this.fetchData()
    },

    handleEdit(e) {
      this.$refs.showInfo.show(e)
    },
    handleDel(e) {
      this.$confirm('此操作将取消订单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        transactionCancel(e).then(res => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '成功!'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        })
      })
    }
  }
}
</script>
